package com.lantosec.hmrp.modules.archives.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.archives.entity.UserBonusPenalty;
import com.lantosec.hmrp.modules.archives.mapper.UserBonusPenaltyMapper;
import com.lantosec.hmrp.modules.archives.service.IUserBonusPenaltyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.archives.vo.UserBonusPenaltyVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.List;

/**
 * <p>
 * 领导干部奖惩情况登记表 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-11
 */
@Service
@Primary
@Log4j2
public class UserBonusPenaltyServiceImpl extends ServiceImpl<UserBonusPenaltyMapper, UserBonusPenalty> implements IUserBonusPenaltyService {

    @Override
    public void saveUserBonus(UserBonusPenaltyVO vo) {
        UserBonusPenalty userBonusPenalty = vo.convertToEntity();
        userBonusPenalty.setUserBonusPenaltyId(UUIDUtils.get());
        //获取当前用户id和名称
        userBonusPenalty.setCreateId(TokenUtil.getUserId());
        userBonusPenalty.setCreateUser(TokenUtil.getName());
        userBonusPenalty.setCreateTime(DateUtils.nowDate());
        userBonusPenalty.setVerifyStatus(1);
        this.save(userBonusPenalty);
    }

    @Override
    public void updateUserBonus(UserBonusPenaltyVO vo) {
        String userBonusPenaltyId = vo.getUserBonusPenaltyId();
        //新增一条数据
        //saveUserBonus(vo);
        if (StringUtils.isNotEmpty(userBonusPenaltyId)) {
            UserBonusPenalty userBonusPenalty = vo.convertToEntity();
            userBonusPenalty.setVerifyStatus(1);
            this.updateById(userBonusPenalty);
        }else {
            saveUserBonus(vo);
        }
    }

    @Override
    public List<UserBonusPenalty> getUserBonusPenalty(String userId) {
        List<UserBonusPenalty> userBonusPenaltyList = this.list(new QueryWrapper<UserBonusPenalty>().eq(UserBonusPenalty.ColName.user_id.toString(), userId).orderByDesc(UserBonusPenalty.ColName.create_time.toString()));
        return userBonusPenaltyList;
    }

    @Override
    public List<UserBonusPenalty> getUserBonusPenaltyVerify(String userId) {
        List<UserBonusPenalty> userBonusPenaltyList = this.list(new QueryWrapper<UserBonusPenalty>().eq(UserBonusPenalty.ColName.user_id.toString(), userId).in(UserBonusPenalty.ColName.verify_status.toString(),0,2));
        return userBonusPenaltyList;
    }

    @Override
    public void verifyUserBonusPenalty(UserBonusPenaltyVO vo) {
        UserBonusPenalty userBonusPenalty = vo.convertToEntity();
        Integer verifyStatus = userBonusPenalty.getVerifyStatus();
        //如果审核不通过
        if (vo.getState() == 0) {
            //审核状态为审核中的变成 已审核
            if (verifyStatus == 2) {
                userBonusPenalty.setVerifyStatus(1);
            }
            //审核状态为未审核的变成 审核不通过
            if (verifyStatus == 0) {
                userBonusPenalty.setVerifyStatus(3);
            }
        }
        //如果审核通过
        if (vo.getState() == 1) {
            //审核状态为审核中的变为 历史记录
            if (verifyStatus == 2) {
                userBonusPenalty.setVerifyStatus(4);
            }
            //审核状态为未审核的变为 已审核
            if (verifyStatus == 0) {
                userBonusPenalty.setVerifyStatus(1);
            }
        }
        //获取当前用户id和名称
        userBonusPenalty.setVerifyId(TokenUtil.getUserId());
        userBonusPenalty.setVerifyUser(TokenUtil.getName());
        userBonusPenalty.setVerifyTime(DateUtils.nowDate());
        this.updateById(userBonusPenalty);
    }

    @Override
    public List<UserBonusPenalty> getUserBonusPenaltyHistory(String userId) {
        List<UserBonusPenalty> userBonusPenaltyList = this.list(new QueryWrapper<UserBonusPenalty>().eq(UserBonusPenalty.ColName.user_id.toString(), userId).eq(UserBonusPenalty.ColName.verify_status.toString(),4));
        return userBonusPenaltyList;
    }
}
